/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package calculo.depreciacao.unidades.fatecpg;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import org.netbeans.microedition.lcdui.SplashScreen;

/**
 * @author Renê
 */
public class CalculadoraMIDlet extends MIDlet implements CommandListener {

    private boolean midletPaused = false;
    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Command LimparCommand;
    private Command exitCommand;
    private Command CalcularCommand;
    private Form form;
    private TextField UnidadesEstimadasTextField;
    private TextField UnidadesProduzidasTextField;
    private TextField CustotextField;
    private StringItem ResultadostringItem;
    private ImageItem LogoimageItem;
    private SplashScreen splashScreen;
    private Font font1;
    private Ticker ticker;
    private Image logo_calc;
    private Font font;
    private Image logo_splash;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The HelloMIDlet constructor.
     */
    public CalculadoraMIDlet() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|
    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getSplashScreen());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == form) {//GEN-BEGIN:|7-commandAction|1|25-preAction
            if (command == CalcularCommand) {//GEN-END:|7-commandAction|1|25-preAction
                try {
                    double unidadesproduzidas = Double.parseDouble(UnidadesProduzidasTextField.getString());
                    double unidadesestimadas = Double.parseDouble(UnidadesEstimadasTextField.getString());
                    double customaquina = Double.parseDouble(CustotextField.getString());
                    double porcentagem = (100 * unidadesproduzidas) / unidadesestimadas;
                    double depreciação = (customaquina * (porcentagem / 100));
                    ResultadostringItem.setText("A Depreciação foi de: " + porcentagem + "%, ou $" + String.valueOf(depreciação));
                } catch (Exception ex) {
                    Alert erro = new Alert("Erro");
                    erro.setString(ex.getMessage());
                    erro.setTimeout(3000);
                    getDisplay().setCurrent(erro);
                }

//GEN-LINE:|7-commandAction|2|25-postAction
                // write post-action user code here
            } else if (command == LimparCommand) {//GEN-LINE:|7-commandAction|3|66-preAction
                ResultadostringItem.setText(null);
                CustotextField.setString("");
                UnidadesEstimadasTextField.setString("");
                UnidadesProduzidasTextField.setString("");


//GEN-LINE:|7-commandAction|4|66-postAction
                // write post-action user code here
            } else if (command == exitCommand) {//GEN-LINE:|7-commandAction|5|19-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|6|19-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|7|41-preAction
        } else if (displayable == splashScreen) {
            if (command == SplashScreen.DISMISS_COMMAND) {//GEN-END:|7-commandAction|7|41-preAction
                // write pre-action user code here
                switchDisplayable(null, getForm());//GEN-LINE:|7-commandAction|8|41-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|9|7-postCommandAction
        }//GEN-END:|7-commandAction|9|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|10|
    //</editor-fold>//GEN-END:|7-commandAction|10|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|18-getter|0|18-preInit
    /**
     * Returns an initiliazed instance of exitCommand component.
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|18-getter|0|18-preInit
            // write pre-init user code here
            exitCommand = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|18-getter|1|18-postInit
            // write post-init user code here
        }//GEN-BEGIN:|18-getter|2|
        return exitCommand;
    }
    //</editor-fold>//GEN-END:|18-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: form ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initiliazed instance of form component.
     * @return the initialized component instance
     */
    public Form getForm() {
        if (form == null) {//GEN-END:|14-getter|0|14-preInit
            // write pre-init user code here
            form = new Form("Deprecia\u00E7\u00E3o por Unidades Produzidas", new Item[] { getLogoimageItem(), getResultadostringItem(), getCustotextField(), getUnidadesEstimadasTextField(), getUnidadesProduzidasTextField() });//GEN-BEGIN:|14-getter|1|14-postInit
            form.addCommand(getExitCommand());
            form.addCommand(getCalcularCommand());
            form.addCommand(getLimparCommand());
            form.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit
            // write post-init user code here
        }//GEN-BEGIN:|14-getter|2|
        return form;
    }
    //</editor-fold>//GEN-END:|14-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: CalcularCommand ">//GEN-BEGIN:|24-getter|0|24-preInit
    /**
     * Returns an initiliazed instance of CalcularCommand component.
     * @return the initialized component instance
     */
    public Command getCalcularCommand() {
        if (CalcularCommand == null) {//GEN-END:|24-getter|0|24-preInit
            // write pre-init user code here
            CalcularCommand = new Command("Calcular", Command.ITEM, 0);//GEN-LINE:|24-getter|1|24-postInit
            // write post-init user code here
        }//GEN-BEGIN:|24-getter|2|
        return CalcularCommand;
    }
    //</editor-fold>//GEN-END:|24-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: UnidadesProduzidasTextField ">//GEN-BEGIN:|22-getter|0|22-preInit
    /**
     * Returns an initiliazed instance of UnidadesProduzidasTextField component.
     * @return the initialized component instance
     */
    public TextField getUnidadesProduzidasTextField() {
        if (UnidadesProduzidasTextField == null) {//GEN-END:|22-getter|0|22-preInit
            // write pre-init user code here
            UnidadesProduzidasTextField = new TextField("N\u00FAmero de unidades produzidas no per\u00EDodo", "", 32, TextField.DECIMAL);//GEN-LINE:|22-getter|1|22-postInit
            // write post-init user code here
        }//GEN-BEGIN:|22-getter|2|
        return UnidadesProduzidasTextField;
    }
    //</editor-fold>//GEN-END:|22-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: UnidadesEstimadasTextField ">//GEN-BEGIN:|23-getter|0|23-preInit
    /**
     * Returns an initiliazed instance of UnidadesEstimadasTextField component.
     * @return the initialized component instance
     */
    public TextField getUnidadesEstimadasTextField() {
        if (UnidadesEstimadasTextField == null) {//GEN-END:|23-getter|0|23-preInit
            // write pre-init user code here
            UnidadesEstimadasTextField = new TextField("N\u00FAmero de unidades estimadas a serem produzidas", "", 32, TextField.DECIMAL);//GEN-LINE:|23-getter|1|23-postInit
            // write post-init user code here
        }//GEN-BEGIN:|23-getter|2|
        return UnidadesEstimadasTextField;
    }
    //</editor-fold>//GEN-END:|23-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: ResultadostringItem ">//GEN-BEGIN:|26-getter|0|26-preInit
    /**
     * Returns an initiliazed instance of ResultadostringItem component.
     * @return the initialized component instance
     */
    public StringItem getResultadostringItem() {
        if (ResultadostringItem == null) {//GEN-END:|26-getter|0|26-preInit
            // write pre-init user code here
            ResultadostringItem = new StringItem("", "", Item.HYPERLINK);//GEN-BEGIN:|26-getter|1|26-postInit
            ResultadostringItem.setFont(getFont1());//GEN-END:|26-getter|1|26-postInit
            // write post-init user code here
        }//GEN-BEGIN:|26-getter|2|
        return ResultadostringItem;
    }
    //</editor-fold>//GEN-END:|26-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: CustotextField ">//GEN-BEGIN:|27-getter|0|27-preInit
    /**
     * Returns an initiliazed instance of CustotextField component.
     * @return the initialized component instance
     */
    public TextField getCustotextField() {
        if (CustotextField == null) {//GEN-END:|27-getter|0|27-preInit
            // write pre-init user code here
            CustotextField = new TextField("Custo do Equipamento", "", 32, TextField.DECIMAL);//GEN-LINE:|27-getter|1|27-postInit
            // write post-init user code here
        }//GEN-BEGIN:|27-getter|2|
        return CustotextField;
    }
    //</editor-fold>//GEN-END:|27-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: ticker ">//GEN-BEGIN:|33-getter|0|33-preInit
    /**
     * Returns an initiliazed instance of ticker component.
     * @return the initialized component instance
     */
    public Ticker getTicker() {
        if (ticker == null) {//GEN-END:|33-getter|0|33-preInit
            // write pre-init user code here
            ticker = new Ticker("");//GEN-LINE:|33-getter|1|33-postInit
            // write post-init user code here
        }//GEN-BEGIN:|33-getter|2|
        return ticker;
    }
    //</editor-fold>//GEN-END:|33-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: splashScreen ">//GEN-BEGIN:|40-getter|0|40-preInit
    /**
     * Returns an initiliazed instance of splashScreen component.
     * @return the initialized component instance
     */
    public SplashScreen getSplashScreen() {
        if (splashScreen == null) {//GEN-END:|40-getter|0|40-preInit
            // write pre-init user code here

            splashScreen = new SplashScreen(getDisplay());//GEN-BEGIN:|40-getter|1|40-postInit
            splashScreen.setTitle("Calculadora");
            splashScreen.setCommandListener(this);
            splashScreen.setImage(getLogo_splash());//GEN-END:|40-getter|1|40-postInit




            // write post-init user code here
        }//GEN-BEGIN:|40-getter|2|
        return splashScreen;
    }
    //</editor-fold>//GEN-END:|40-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: LogoimageItem ">//GEN-BEGIN:|56-getter|0|56-preInit
    /**
     * Returns an initiliazed instance of LogoimageItem component.
     * @return the initialized component instance
     */
    public ImageItem getLogoimageItem() {
        if (LogoimageItem == null) {//GEN-END:|56-getter|0|56-preInit
            // write pre-init user code here
            LogoimageItem = new ImageItem("", getLogo_calc(), ImageItem.LAYOUT_DEFAULT, "<Imagem perdida>");//GEN-LINE:|56-getter|1|56-postInit
            // write post-init user code here
        }//GEN-BEGIN:|56-getter|2|
        return LogoimageItem;
    }
    //</editor-fold>//GEN-END:|56-getter|2|
    //</editor-fold>

    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: logo_splash ">//GEN-BEGIN:|61-getter|0|61-preInit
    /**
     * Returns an initiliazed instance of logo_splash component.
     * @return the initialized component instance
     */
    public Image getLogo_splash() {
        if (logo_splash == null) {//GEN-END:|61-getter|0|61-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|61-getter|1|61-@java.io.IOException
                logo_splash = Image.createImage("/calculo/depreciacao/unidades/fatecpg/logo.png");
            } catch (java.io.IOException e) {//GEN-END:|61-getter|1|61-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|61-getter|2|61-postInit
            // write post-init user code here
        }//GEN-BEGIN:|61-getter|3|
        return logo_splash;
    }
    //</editor-fold>//GEN-END:|61-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: font ">//GEN-BEGIN:|62-getter|0|62-preInit
    /**
     * Returns an initiliazed instance of font component.
     * @return the initialized component instance
     */
    public Font getFont() {
        if (font == null) {//GEN-END:|62-getter|0|62-preInit
            // write pre-init user code here
            font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD | Font.STYLE_UNDERLINED, Font.SIZE_MEDIUM);//GEN-LINE:|62-getter|1|62-postInit
            // write post-init user code here
        }//GEN-BEGIN:|62-getter|2|
        return font;
    }
    //</editor-fold>//GEN-END:|62-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: logo_calc ">//GEN-BEGIN:|63-getter|0|63-preInit
    /**
     * Returns an initiliazed instance of logo_calc component.
     * @return the initialized component instance
     */
    public Image getLogo_calc() {
        if (logo_calc == null) {//GEN-END:|63-getter|0|63-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|63-getter|1|63-@java.io.IOException
                logo_calc = Image.createImage("/calculo/depreciacao/unidades/fatecpg/logocalc.png");
            } catch (java.io.IOException e) {//GEN-END:|63-getter|1|63-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|63-getter|2|63-postInit
            // write post-init user code here
        }//GEN-BEGIN:|63-getter|3|
        return logo_calc;
    }
    //</editor-fold>//GEN-END:|63-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: font1 ">//GEN-BEGIN:|64-getter|0|64-preInit
    /**
     * Returns an initiliazed instance of font1 component.
     * @return the initialized component instance
     */
    public Font getFont1() {
        if (font1 == null) {//GEN-END:|64-getter|0|64-preInit
            // write pre-init user code here
            font1 = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);//GEN-LINE:|64-getter|1|64-postInit
            // write post-init user code here
        }//GEN-BEGIN:|64-getter|2|
        return font1;
    }
    //</editor-fold>//GEN-END:|64-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: LimparCommand ">//GEN-BEGIN:|65-getter|0|65-preInit
    /**
     * Returns an initiliazed instance of LimparCommand component.
     * @return the initialized component instance
     */
    public Command getLimparCommand() {
        if (LimparCommand == null) {//GEN-END:|65-getter|0|65-preInit
            // write pre-init user code here
            LimparCommand = new Command("Limpar", Command.ITEM, 0);//GEN-LINE:|65-getter|1|65-postInit
            // write post-init user code here
        }//GEN-BEGIN:|65-getter|2|
        return LimparCommand;
    }
    //</editor-fold>//GEN-END:|65-getter|2|

    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet();
        } else {
            initialize();
            startMIDlet();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }
}
